package com.tvein.sanguo.controller.Game.user
{
	//flash
	import flash.utils.ByteArray;
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.command.SimpleFabricationCommand;
	//custom
	import com.tvein.sanguo.common.CommonConstants;
	import com.tvein.sanguo.view.ApplicationMediator;
	import com.tvein.sanguo.model.ServerProxy;
	import com.tvein.sanguo.model.FilesProxy;
	import com.tvein.sanguo.model.SFSProxy;
//
	public class GameStart extends SimpleFabricationCommand implements ICommand
	{
		private var sanguoMediator:ApplicationMediator;
		private var serverData:ServerProxy;
		private var filesData:FilesProxy;
		private var sfsData:SFSProxy;
		
		override public function execute(notification:INotification) : void
		{
			sanguoMediator = facade.retrieveMediator(ApplicationMediator.NAME) as ApplicationMediator;
			
			serverData = facade.retrieveProxy(ServerProxy.NAME) as ServerProxy;
			filesData = facade.retrieveProxy(FilesProxy.NAME) as FilesProxy;
			sfsData = facade.retrieveProxy(SFSProxy.NAME) as SFSProxy;
			
			routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["show", false]], null, "*");
			
			if (!sfsData.sfs.isConnected)
			{
				sfsData.sfs.connect(serverData.sfsServer[0], serverData.sfsServer[1]);
				
				sendNotification(CommonConstants.SYSTEM_CONSOLE, ["-gameStatus", [1, "系统连接中，请稍候..."]]);
			}
			else
			{
				switch(sfsData.sfs.currentZone.toString())
				{
					case "Tvein":
						sendNotification(CommonConstants.SYSTEM_CONSOLE, ["-tveinLogin"]);
						break;
					case "SanGuo":
						sendNotification(CommonConstants.GAME_CONSOLE, ["-roleList"]);
						break;
				}
			}
			
			sanguoMediator.sanguo.setChildIndex(sanguoMediator.sanguo.getChildByName(serverData.getSwfInstance(CommonConstants.MODULE_BACKGROUND)), 1);
			sanguoMediator.sanguo.setChildIndex(sanguoMediator.sanguo.getChildByName(serverData.getSwfInstance(CommonConstants.MODULE_TOOL)), 2);
			sanguoMediator.sanguo.setChildIndex(sanguoMediator.sanguo.getChildByName(serverData.getSwfInstance(CommonConstants.MODULE_ZONE)), 3);
			sanguoMediator.sanguo.setChildIndex(sanguoMediator.sanguo.getChildByName(serverData.getSwfInstance(CommonConstants.MODULE_PROMPT)), 4);
			sanguoMediator.sanguo.setChildIndex(sanguoMediator.sanguo.getChildByName(serverData.getSwfInstance(CommonConstants.MODULE_ROOM)), 5);
			sanguoMediator.sanguo.setChildIndex(sanguoMediator.sanguo.getChildByName(serverData.getSwfInstance(CommonConstants.MODULE_CROWD)), 6);
			sanguoMediator.sanguo.setChildIndex(sanguoMediator.sanguo.getChildByName(serverData.getSwfInstance(CommonConstants.MODULE_CHARACTER)), 7);
			sanguoMediator.sanguo.setChildIndex(sanguoMediator.sanguo.getChildByName(serverData.getSwfInstance(CommonConstants.MODULE_EVENT)), 8);
			sanguoMediator.sanguo.setChildIndex(sanguoMediator.sanguo.getChildByName(serverData.getSwfInstance(CommonConstants.MODULE_ACTION)), 9);
			sanguoMediator.sanguo.setChildIndex(sanguoMediator.sanguo.getChildByName(serverData.getSwfInstance(CommonConstants.MODULE_CHAT)), 10);
			
			sanguoMediator.sysLog.textColor = 0x000000;
			
			var fileSrc:String;
			var tmpByteArray:ByteArray = new ByteArray();
			
			for (var i:int = 0; i < filesData.filesArray.length; i++)
			{
				fileSrc = filesData.filesArray[i][0].split("|")[0];
				
				switch (fileSrc)
				{
					case "/SanGuo/shell/background/Start/Start.swf":
						tmpByteArray.writeBytes(filesData.filesArray[i][1]);
						routeNotification(CommonConstants.MODULE_INVOKE, ["-load", ["start.swf", tmpByteArray]], null, CommonConstants.MODULE_BACKGROUND + "/*");
						break;
					case "/SanGuo/sound/music/BGMusic.mp3":
						tmpByteArray.writeBytes(filesData.filesArray[i][1]);
						routeNotification(CommonConstants.MODULE_INVOKE, ["-music", [filesData.filesArray[i][0], tmpByteArray]], null, CommonConstants.MODULE_ZONE + "/*");
						break;
				}
				
				tmpByteArray.clear();
			}
			
			i = Math.floor(Math.random() * serverData.srcServer.length());
			routeNotification(CommonConstants.MODULE_INVOKE, ["-init", ["srcServer", serverData.srcServer[i].@address]], null, "*");
		}
		
		//Custom
	}
}